package doublepointer;

/**
 * Created by lh on 2019/7/22.
 */
public class LongestWord {

    public static String longestWord(String str, String[] targets) {
        String longgetsWord = "";
        for (String target : targets) {
            int wordLength = longgetsWord.length(), targetLength = target.length();
            if (wordLength > targetLength || (wordLength == targetLength && longgetsWord.compareTo(target) < 0)) {
                continue;
            }
            if (isSubstr(str, target)) {
                longgetsWord = target;
            }
        }
        return longgetsWord;
    }

    public static boolean isSubstr(String str, String target) {
        int i = 0, j = 0;
        while (i < str.length() && j < target.length()) {
            if (str.charAt(i) == target.charAt(j)) {
                j++;
            }
            i++;
        }
        return j == target.length();
    }
}
